package com.jzg.jcptadmin.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.view.MyErrorLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WebViewActivity extends BaseActivity {
    @Bind(R.id.webview)
    WebView webview;
    @Bind(R.id.ll_error)
    MyErrorLayout llError;
    @Bind(R.id.title_content)
    TextView titleContent;
    @Bind(R.id.psb)
    ProgressBar psb;
    private boolean isShowTitle;
    private String path;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        //设置javascript支持
        webview.getSettings().setJavaScriptEnabled(true);
        WebSettings settings = webview.getSettings();
        settings.setDomStorageEnabled(true);

        Bundle bd = getIntent().getExtras();
        final int webViewTitle = getIntent().getIntExtra(Constant.WEBVIEW_TITLE, 1);//1显示自定义title 2显示链接title
        path = bd == null ? "" : bd.getString("path");
        title = bd == null ? "" : bd.getString("title");
        titleContent.setMaxLines(1);
        titleContent.setEllipsize(TextUtils.TruncateAt.END);
        //webview.addJavascriptInterface(new JsInterface(this), "JsInterface");

        webview.loadUrl(path);
        if (webViewTitle == 1 && !TextUtils.isEmpty(title)) {
            titleContent.setText(title);
        }

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                psb.setVisibility(View.VISIBLE);
            }


            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                llError.setVisibility(View.VISIBLE);
                isShowTitle = true;
            }
        });

        webview.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                psb.setProgress(newProgress);
                if (newProgress == 100) {
                    psb.setVisibility(View.GONE);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (webViewTitle == 2) {
                    if (!TextUtils.isEmpty(title) && titleContent != null) {
                        titleContent.setText(title);
                    }
                }
            }
        });

        llError.setOnReloadClickLintener(new MyErrorLayout.ReloadClickLintener() {
            @Override
            public void onReload() {
                llError.setVisibility(View.GONE);
                isShowTitle = false;
                webview.loadUrl(path);
            }
        });
    }

    @OnClick({R.id.title_return, R.id.webview, R.id.ll_error})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.webview:
                break;
            case R.id.ll_error:
                break;
            case R.id.title_return:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
